<template>
	<view>
		<view>
			<table-title content="退换租办理表详情"></table-title>
			<view class="record-id">单号：{{processNo}}</view>
			<view class="in-center">
				<small-title content="基本资料"></small-title>
				<view class="state-bar">
					<view class="state-msg">{{state}}</view>
				</view>
			</view>
			<view class="item-table">
				<view-item name="楼盘" :value="userMsg.userBuilding"></view-item>
				<view-item name="楼层" :value="userMsg.userFloor" :isLast="true"></view-item>
			</view>
			<view>
				<small-title :isNeed="true" content="退换租类别"></small-title>
				<radio-group class="radiogroup-style" :disabled="!(state==='待申请')">
					<view class="first-radio">
						<radio @click="chooseReturn(0)" :disabled="!(state==='待申请')" :checked="returnChecked" color="#FF0000"></radio>
						<view>退租</view>
					</view>
					<view class="second-radio">
						<radio @click="chooseReturn(1)" :disabled="!(state==='待申请')" :checked="!returnChecked" color="#FF0000"></radio>
						<view>换租</view>
					</view>
				</radio-group>
			</view>
			<view v-if="status > 4 && status < 9">
				<small-title content="费用结算清单"></small-title>
				<view class="download-text" @click="downloadFile(0)">费用结算清单.docx</view>
				<view class="cost-all">费用合计：{{allPrice}} 元</view>
			</view>
			<view v-if="allPrice !== 0">
				<small-title :isNeed="true" content="支付信息截图"></small-title>
				<image :class="chargeImg=='/static/uploadImg.png' ? 'pay-image' : 'pay-image-border'" :src="chargeImg" mode="scaleToFill" @tap='chooseImage'></image>
			</view>
			<small-title content="处理进度"></small-title>
			<proccess-bar v-if="processFlag" :proccessList="proccessListDefult"></proccess-bar>
			<view v-if="status===3" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="qfConfirm(0)">自行复原</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="qfConfirm(4)">博邦复原</button>
				</view>
			</view>
			<view v-if="status===5" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="qfCostConfirm">确认缴费</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="back">返回</button>
				</view>
			</view>
			<view v-if="state==='待申请'" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="addOrder">发起申请</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="back">返回</button>
				</view>
			</view>
			<view style="height: 200rpx;"></view>
		</view>
		<uni-popup ref="popup" type="dialog">
		    <uni-popup-dialog mode="input" message="成功消息" title="请输入整改意见" :duration="2000" :before-close="true" @close="close" @confirm="confirm"></uni-popup-dialog>
		</uni-popup>
		<l-file
		    ref="lFile" 
			@up-success="upSuccess"
		></l-file>
	</view>
</template>		

<script>
	import {
		ONLINE_SERVER_ADDRESS_BASE,
		syncGetOrder,
		myUploadFile
	} from "../../../util/network.js"
	import {
		processHistoryList
	} from "../../../util/common.js"
	export default{
		data() {
			return {
				processFlag:false,
				status:-1,
				recordId: -1,
				processNo:'待分配',
				state: '已受理',
				userMsg:{'userName':'','userPhone':'','userCompany':'','userBuilding':'',
					'userFloor':'','userRoom':''},
				returnChecked:true,
				docxFileId:0,
				pdfFileId:0,
				chargeImg:'/static/uploadImg.png',                                     //支付信息截图
				chargeImgId:-1,                                     //支付信息截图
				nopassReasons:'',
				proccessListDefult:[],
				token:'',
				allPrice:0,
			}
		},
		methods: {
			open() {
				this.$refs.popup.open()
			},
			/**
			 * 点击取消按钮触发
			 * @param {Object} done
			 */
			close() {
				// TODO 做一些其他的事情，before-close 为true的情况下，手动执行 close 才会关闭对话框
				// ...
				this.$refs.popup.close()
			},
			/**
			 * 点击确认按钮触发
			 * @param {Object} done
			 * @param {Object} value
			 */
			confirm(value) {
				this.$refs.popup.close()
				if(value == ""){
					uni.showModal({
						content:'意见不能为空',
						showCancel:false
					});
					this.$refs.popup.close()
				}else{
					this.nopassReasons = "需要整改，描述：" + value
					this.qfConfirm(1)
					// TODO 做一些其他的事情，手动执行 close 才会关闭对话框
					// ...
					this.$refs.popup.close()
				}
			},
			
			async loadImg(chargeFileId){
				// let _this = this
				const res = await uni.downloadFile({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/file/download/` + chargeFileId,
					header: {
						'Authorization': this.token,
					},
				})
				return new Promise((resolve, reject) => {
				        resolve(res[1].tempFilePath)
				})
			},
			
			chooseReturn(status){
				if(this.state==='待申请'){
					if(status == 0){
						this.returnChecked = true
					}else{
						this.returnChecked = false
					}
				}
			},
			
			downloadFile(fileType){
				let fileId = 0
				if(fileType == 0){
					fileId = this.docxFileId
				}else{
					fileId = this.pdfFileId
				}
				if(fileId == -1){
					uni.showModal({
						content:'找不到该文件,请检查是否已上传。',
						showCancel:false
					})
					return 0
				}
				this.$refs.lFile.download({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/file/download/` + fileId,
					token:this.token,
				})
				.then(path=>{
				    // 打开文件
				    this.$refs.lFile.open(path);
				});
			},
			
			async downloadOrderFile(fileType){
				let fileId = 0
				if(fileType == 0){
					fileId = this.docxFileId
				}else{
					fileId = this.pdfFileId
				}
				const res = await uni.downloadFile({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/file/download/` + fileId,
					header: {
						'Authorization': this.token,
					},
				})
				return new Promise((resolve, reject) => {
				        resolve(res[1].tempFilePath)
				})
			},
			
			chooseImage(){
				console.log("ceshi",this.state)
				if(this.state === '待确认缴费') {
					uni.chooseImage({
						count: 1,
						sourceType: ['album', 'camera'],
						// #ifdef MP-WEIXIN
						sizeType: ['compressed'],
						// #endif
						success: (res) => {
							let a = myUploadFile(this.token, res.tempFilePaths[0])
							 a.then((v)=>{
								 this.chargeImgId = v
								 console.log("zuizhongid",this.chargeImgId)
							})
							this.chargeImg = res.tempFilePaths[0];
						}
					})
				}else{
					uni.previewImage({
						current: 0,
						urls: [this.chargeImg]
					})
				}
			},
			
			qfCostConfirm(){
				console.log("[info]","上传缴费截图：" + this.recordId)
				if(this.chargeImg == '/static/uploadImg.png'){
					uni.showModal({
						content:'请上传支付截图',
						showCancel:false
					});
					return -1
				}
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/surrender/qf-cost-confirm/` + this.recordId,
					header: {
						'Authorization': this.token,
					},
					data: {
						chargeImgId:this.chargeImgId,
						commentMsg: "租户已上传缴费截图",                       //操作信息
					},
					method:'POST',
					success: function (res) {
						console.log("上传缴费截图成功",res);
						if(res.data.code == 200){
							uni.navigateBack()
						}
					},
					fail: (res) => {
						console.log("上传缴费截图调用失败")
					}
				})
			},
			
			qfConfirm(status){
				console.log("[info]","选择复原方式：" + this.recordId)
				let commentMsg = ''
				if(status == 0){
					commentMsg = "租户已选择自行复原，待管家择期安排工程验收"
				}else{
					commentMsg = "租户已选择博邦复原"
				}
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/surrender/qf-recover-confirm/` + this.recordId,
					header: {
						'Authorization': this.token,
					},
					data: {
						status:status,
						commentMsg: commentMsg,                       //操作信息
					},
					method:'POST',
					success: function (res) {
						console.log("选择复原方式成功",res);
						if(res.data.code == 200){
							uni.navigateBack()
						}
					},
					fail: (res) => {
						console.log("选择复原方式调用失败")
					}
				})
			},
		   addOrder(){
		   	console.log("[info]","工单已创建，工单号为：" + this.recordId)
			let status = 0
		   	if(this.returnChecked){
		   		status = 0
		   	}else{
				status = 1
			}
		   	uni.request({
		   		url:`${ONLINE_SERVER_ADDRESS_BASE}/surrender/add`,
		   		header: {
		   			'Authorization': this.token,
		   		},
		   		data: {
					type:status,
		   			commentMsg: '发起申请'                       //操作信息
		   		},
		   		method:'POST',
		   		success: function (res) {
		   			console.log("提交成功",res.data);
					if(res.data.code == 200){
						uni.navigateBack()
					}
		   		}
		   	})
		   },
			getOrder(){
				let a = syncGetOrder("surrender/show/" + this.recordId)
				 a.then((res)=>{
					 console.log("异步",res)
					 if(res.data.code == 200){
						 console.log("详细",res.data.data)
						 // this.pdfFileId = res.data.data.contractId
						 // this.docxFileId = res.data.data.furnitureId
						 this.status = parseInt(res.data.data.status)
						 // console.log(`${ONLINE_SERVER_ADDRESS_BASE}/goods-release/finish/` + this.recordId)
						 switch(this.status) {
						      case 0:
						         this.state = "待管家指派"
						         break;
						      case 1:
						         this.state = "待工程验房"
						         break;
						 	 case 2:
						         this.state = "待管家处理"
						 	    break;
						 	 case 3:
						         this.state = "待租户确认"
						 	    break;
						      case 4:
						         this.state = "待管家上传"
						         break;
						 	 case 5:
						         this.state = "待确认缴费"
						 	    break;
						 	 case 6:
						         this.state = "待财务审核"
						 	    break;
						 	 case 7:
						         this.state = "管家办理中"
						 	    break;
						 	 case 8:
						         this.state = "已办理完毕"
						 	    break;
						      case 9:
						         this.state = "管家已驳回"
						         break;
						      default:
						         this.state = "待申请"
						 } 
						 this.processNo = res.data.data.processNo
						 this.docxFileId = res.data.data.feeConfirmFileId
						 this.chargeImgId = res.data.data.payScreenshotId 
						 if(this.chargeImgId != null){
							 let b = this.loadImg(this.chargeImgId)
							 b.then((v)=>{
								this.chargeImg = v
							 })
						 }
						 if(res.data.data.allPrice > 0){
							 this.allPrice = res.data.data.allPrice
						 }
						 if(res.data.data.isRefund == 0){
						 	this.returnChecked = true
						 }else{
						 	this.returnChecked = false
						 }
						 this.proccessListDefult = processHistoryList(res.data.data.processDetail)
						 this.processFlag = true
					}
				})
			},
			
			back(){
				uni.navigateBack()
			},
		},
		onLoad(option) {
			//传入单号
			let userInfo = uni.getStorageSync('userinfo')
			if(userInfo != null){
				this.userMsg.userBuilding = userInfo.title
				this.userMsg.userFloor = userInfo.floor
				this.recordId = option.id
				this.token = uni.getStorageSync('token')
				this.state = option.state
			}else{
				uni.navigateTo('../../login/login')
			}
		},
		mounted() {
			//如果是新建表单则不查询
			if(this.state != '待申请'){
				this.getOrder()
			}
		}
	}
</script>

<style>
	page{
		background-color: #F1F1F1
	}
	.record-id{
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
	}
	.state-bar{
		display: flex;
		align-items: center;
		color: #FFFFFF;
		background-color: #DD524D;
		width: 260rpx;
		justify-content: center;
		margin-left: auto;
		margin-right: 50rpx;
		font-size: 30rpx;
		border-radius: 50rpx;
		height: 50rpx;
	}
	.state-msg{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.item-table{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.mix-button{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.left-part{
		float: right;
	}
	.right-part{
		float: left;
	}
	.left-button{
		background-color: #FF0000;
		border-radius: 50rpx 0 0 50rpx;
		color: #FFFFFF;
		width: 300rpx;
		font-size: 30rpx;
	}
	.right-button{
		background-color: #0000FF;
		border-radius: 0 50rpx 50rpx 0;
		color: #FFFFFF;
		width: 300rpx;
		font-size: 30rpx;
	}
	.in-center{
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.add-button {
		border-width: 0px;
		align-items: center;
		justify-content: center;
		margin: 30rpx 0 40rpx 250rpx;
		height: 50rpx;
		width: 250rpx;
		display: flex;
		color: #22B242;
	}
	.download-text {
	  -webkit-box-sizing: content-box;
	  -moz-box-sizing: content-box;
	  box-sizing: content-box;
	  border: none;
	  font: normal 16px/1 "Times New Roman", Times, serif;
	  color: rgba(18,25,232,1);
	  text-decoration: underline;
	  -o-text-overflow: ellipsis;
	  text-overflow: ellipsis;
	  margin-left: 60rpx;
	}
	.pay-image{
		margin: 20rpx 0 20rpx 50rpx;
		width: 130rpx;
		height: 130rpx;
	}
	.pay-image-border{
		border:2rpx solid;
		border-radius: 2px;
		margin: 20rpx 0 20rpx 50rpx;
		width: 126rpx;
		height: 126rpx;
	}
	.cost-all{
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
	}
	.radiogroup-style{
		width: 650rpx;
		height: 100rpx;
		background-color: #FFFFFF;
		display: flex;
		margin-left: 50rpx;
		justify-content: center;
		border: solid 1rpx #C0C0C0;
	}
	.textarea-style{
		display: flex;
		justify-content: center;
		width: 650rpx;
		height: 300rpx;
		margin-left: 50rpx;
		background-color: #FFFFFF;
		border: solid 1rpx #C0C0C0;
	}
	.first-radio{
		display: flex;
		height: 100rpx;
		width: 300rpx;
		align-items: center;
	}
	.second-radio{
		display: flex;
		height: 100rpx;
		align-items: center;
		margin-left: 50rpx;
	}
</style>
